﻿using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace wpf_print.view
{
    internal class InitView
    {
        private static JArray curTableList = new();
        private static List<JObject> columnList = new();
        private static List<JObject> topList = new();
        private static JObject titleTop = new();
        private static List<JObject> tailList = new();
        private static JArray curContainerTableList = new();
        private static List<JObject> containerTailList = new();
        private static List<JObject> containerTopList = new();
        private static JObject containerTitle = new();
        private static List<JObject> containerColumnList = new();
        private static readonly JObject pageInfo = new();
        private static JObject paperDetail = new();
        private static bool isLast = false;
        private static void FormatePageData(JObject data, int pageNum) {
            curTableList = new JArray();
            columnList = new List<JObject>();
            topList = new List<JObject>();
            tailList = new List<JObject>();
            List<JArray> tableList = new();
            // 表格数据
            var a = (JArray)data["detailPage"]["pages"]["pages"];
            foreach (object item in a)
            {
                var cur = (JObject)item;
                tableList.Add((JArray)cur["items"]);
            }
            curTableList = tableList[pageNum];
            for (var i = 0; i < curTableList.Count; ++i)
            {
                curTableList[i]["number"] = i + 1;
            }
            // 合计
            if (isLast)
            {
                var lastObj = new JObject();
                var totalMap = (JObject)data["detailPage"]["pages"]["totalMap"];
                lastObj.Add("number", "合计");
                foreach (JProperty item in totalMap.Properties()) {
                    lastObj.Add(item.Name, item.Value);
                }
                
                curTableList.Add(lastObj);
            }
            pageInfo["page"] = a[pageNum];
            pageInfo["curPageNum"] = pageNum + 1;
            pageInfo["allPageNum"] = a.Count;
            // 表格头
            foreach (object item in data["detailPage"]["table"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                columnList.Add(cur);
            }
            // 页面头部
            foreach (object item in data["detailPage"]["top"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                var column = (double)cur["column"];
                cur["cssStyle"]["width"] = 740 / column;
                topList.Add((JObject)cur);
            }
            //  页面标题
            titleTop = topList[0];
            var columnTopTitle = (double)titleTop["column"];
            titleTop["cssStyle"]["width"] = 740 / columnTopTitle - 80;
            topList.RemoveRange(0, 1);
            // 页面尾部
            foreach (object item in data["detailPage"]["foot"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                var column = (double)cur["column"];
                cur["cssStyle"]["width"] = 740 / column;
                tailList.Add((JObject)cur);
            }
            var curPaper = (JObject)((JArray)data["detailPage"]["paperDetail"])[0];
            curPaper["cssStyle"] = JObject.Parse((string)curPaper["css"]);
            var curColumn = (double)curPaper["column"];
            curPaper["cssStyle"]["width"] = 740 / curColumn;
            paperDetail = (JObject)curPaper;
        }
        private static void FormateContainerData(JObject data)
        {
            curContainerTableList = new JArray();
            containerColumnList = new List<JObject>();
            containerTopList = new List<JObject>();
            containerTailList = new List<JObject>();
            // 框单表格数据
            foreach (object item in data["containerPage"]["containerDetail"])
            {
                var cur = (JObject)item;
                curContainerTableList.Add(cur);
            }
            // 框单表格头
            foreach (object item in data["containerPage"]["container"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                containerColumnList.Add(cur);
            }
            // 框单页面头
            foreach (object item in data["containerPage"]["top"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                var column = (double)cur["column"];
                cur["cssStyle"]["width"] = 740 / column;
                containerTopList.Add((JObject)cur);
            }
            // 框单页面标题
            containerTitle = containerTopList[0];
            var columnTitle = (double)containerTitle["column"];
            containerTitle["cssStyle"]["width"] = 740 / columnTitle - 80;
            containerTopList.RemoveRange(0, 1);
            foreach (object item in data["containerPage"]["foot"])
            {
                var cur = (JObject)item;
                cur["cssStyle"] = JObject.Parse((string)cur["css"]);
                var column = (double)cur["column"];
                cur["cssStyle"]["width"] = 740 / column;
                containerTailList.Add((JObject)cur);
            }
        }
        private static void FormateData(JObject data, int pageNum,string type) {
            if (type == "page") {
                FormatePageData(data, pageNum);
            }




            if (type == "container") {
                FormateContainerData(data);
            }
            if (type == "containerPage") {
                FormateContainerData(data);
                FormatePageData(data, pageNum);
            }

            pageInfo["moreData"] = (JObject)data["moreData"];
            pageInfo["isContainerData"] = (JObject)data["moreData"]["isContainerData"];
        }
        public static Grid GetView(JObject data,int pageNum,bool last) {
            string type;
            isLast = last;
            if ((bool)data["isContainerData"] && (string)data["moreData"]["containerPlace"] == "CONTAINER_PLACE_NONE")
            {
                type = "container";
            }
            else
            {
                if ((bool)data["isContainerData"] && (string)data["moreData"]["containerPlace"] == "CONTAINER_PLACE_HEAD")
                {
                    type = "containerPage";
                }
                else {
                    type = "page";
                }
                
            }
            FormateData(data, pageNum,type);

            if(type == "page"){
                return GetPageView();
            }
            if(type == "container"){
                return GetContainerView();
            }
            if (type == "containerPage")
            {
                return GetPageContainerView(pageNum);
            }
            return new Grid();
        }
        public static Grid GetPageView()
        {
            // 获取和载入数据
            Grid containerGrid = new();
            for (var i = 0; i < 6; ++i)
            {
                RowDefinition rd = new();
                containerGrid.RowDefinitions.Add(rd);
            }

            ColumnDefinition cd = new();
            containerGrid.ColumnDefinitions.Add(cd);

            DataGrid dataGrid = TableView.GetDataTableDom(curTableList, columnList);
            dataGrid.Margin = new Thickness(0, 8, 0, 8);
            var topGrid = ListView.GetListView(topList);
            var tailGrid = ListView.GetListView(tailList);
            var titleGrid = TitleView.GeTitleView(titleTop, pageInfo);
            var footView = FootView.GeFootView(pageInfo);
            var paperView = PaperView.GePaperView(paperDetail);
            Grid.SetRow(titleGrid, 0);
            Grid.SetRow(topGrid, 1);
            Grid.SetRow(dataGrid, 2);
            Grid.SetRow(tailGrid, 3);
            Grid.SetRow(paperView, 4);
            Grid.SetRow(footView, 5);
            containerGrid.Children.Add(titleGrid);
            containerGrid.Children.Add(topGrid);
            containerGrid.Children.Add(dataGrid);
            containerGrid.Children.Add(tailGrid);
            containerGrid.Children.Add(paperView);
            containerGrid.Children.Add(footView);
            return containerGrid;
        }
        public static Grid GetContainerView()
        {
            // 获取和载入数据
            Grid containerGrid = new();
            for (var i = 0; i < 5; ++i)
            {
                RowDefinition rd = new();
                containerGrid.RowDefinitions.Add(rd);
            }

            ColumnDefinition cd = new();
            containerGrid.ColumnDefinitions.Add(cd);

            DataGrid dataGrid = TableView.GetDataTableDom(curContainerTableList, containerColumnList);
            dataGrid.Margin = new Thickness(0, 8, 0, 8);
            var topGrid = ListView.GetListView(containerTopList);
            var tailGrid = ListView.GetListView(containerTailList);
            var titleGrid = TitleView.GeTitleView(containerTitle, pageInfo);
            var footView = FootView.GeFootView(pageInfo);
            Grid.SetRow(titleGrid, 0);
            Grid.SetRow(topGrid, 1);
            Grid.SetRow(dataGrid, 2);
            Grid.SetRow(tailGrid, 3);
            Grid.SetRow(footView, 4);
            containerGrid.Children.Add(titleGrid);
            containerGrid.Children.Add(topGrid);
            containerGrid.Children.Add(dataGrid);
            containerGrid.Children.Add(tailGrid);
            containerGrid.Children.Add(footView);
            return containerGrid;
        }
        public static Grid GetPageContainerView(int pageNum)
        {
            // 获取和载入数据
            Grid containerGrid = new();
            int rowNum = 6;
            if (pageNum == 0) {
                rowNum = 7;


            }
            for (var i = 0; i < rowNum; ++i)
            {
                RowDefinition rd = new();
                containerGrid.RowDefinitions.Add(rd);
            }

            ColumnDefinition cd = new();
            containerGrid.ColumnDefinitions.Add(cd);

            DataGrid dataGrid = TableView.GetDataTableDom(curTableList, columnList);
            dataGrid.Margin = new Thickness(0, 8, 0, 8);
            DataGrid dataContainerGrid = TableView.GetDataTableDom(curContainerTableList, containerColumnList);
            dataContainerGrid.Margin = new Thickness(0, 8, 0, 0);
            var topGrid = ListView.GetListView(topList);
            var tailGrid = ListView.GetListView(tailList);
            var titleGrid = TitleView.GeTitleView(titleTop, pageInfo);
            var footView = FootView.GeFootView(pageInfo);
            var paperView = PaperView.GePaperView(paperDetail);
            Grid.SetRow(titleGrid, 0);
            Grid.SetRow(topGrid, 1);
            if (pageNum == 0)
            {
                Grid.SetRow(dataContainerGrid, 2);
                Grid.SetRow(dataGrid, 3);
                Grid.SetRow(tailGrid, 4);
                Grid.SetRow(paperView, 5);
                Grid.SetRow(footView, 6);

            }
            else {
                Grid.SetRow(dataGrid, 2);
                Grid.SetRow(tailGrid, 3);
                Grid.SetRow(paperView, 4);
                Grid.SetRow(footView, 5);
            }
            
            
            containerGrid.Children.Add(titleGrid);
            containerGrid.Children.Add(topGrid);
            if (pageNum == 0)
            {
                containerGrid.Children.Add(dataContainerGrid);

            }
            
            containerGrid.Children.Add(dataGrid);
            containerGrid.Children.Add(tailGrid);
            containerGrid.Children.Add(paperView);
            containerGrid.Children.Add(footView);
            return containerGrid;
        }
    }
}
